// EjerforholdForm.cpp : implementation file
//

#include "stdafx.h"
#include "EBASApp.h"
#include "EjerforholdForm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CEjerforholdForm

IMPLEMENT_DYNCREATE(CEjerforholdForm, CFormView)

CEjerforholdForm::CEjerforholdForm()
	: CFormView(CEjerforholdForm::IDD)
{
	//{{AFX_DATA_INIT(CEjerforholdForm)
	m_firma = _T("");
	m_adresse = _T("");
	m_epost = _T("");
	m_postNr = _T("");
	m_telefax = _T("");
	m_telefon = _T("");
	m_guid = _T("");
	m_leder = _T("");
	//}}AFX_DATA_INIT

	m_pAdresse = NULL;
	m_pEjerforhold = NULL;
}

CEjerforholdForm::~CEjerforholdForm()
{
}

void CEjerforholdForm::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEjerforholdForm)
	DDX_Control(pDX, IDC_COMBO_FIRMA_BY, m_by);
	DDX_Control(pDX, IDC_LIST_LEDERE, m_ledere);
	DDX_Control(pDX, IDC_COMBO_ADRESSE_TYPE, m_adresseType);
	DDX_Text(pDX, IDC_EDIT_FIRMA, m_firma);
	DDX_Text(pDX, IDC_EDIT_FIRMA_ADRESSE, m_adresse);
	DDX_Text(pDX, IDC_EDIT_FIRMA_EPOST, m_epost);
	DDX_Text(pDX, IDC_EDIT_FIRMA_POSTNR, m_postNr);
	DDX_Text(pDX, IDC_EDIT_FIRMA_TELEFAX, m_telefax);
	DDX_Text(pDX, IDC_EDIT_FIRMA_TELEFON, m_telefon);
	DDX_Text(pDX, IDC_EDIT_GUID, m_guid);
	DDX_Text(pDX, IDC_EDIT_LEDER, m_leder);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CEjerforholdForm, CFormView)
	//{{AFX_MSG_MAP(CEjerforholdForm)
	ON_EN_KILLFOCUS(IDC_EDIT_FIRMA_POSTNR, OnKillfocusEditFirmaPostnr)
	ON_CBN_KILLFOCUS(IDC_COMBO_FIRMA_BY, OnKillfocusComboFirmaBy)
	ON_LBN_DBLCLK(IDC_LIST_LEDERE, OnDblclkListLedere)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEjerforholdForm diagnostics

#ifdef _DEBUG
void CEjerforholdForm::AssertValid() const
{
	CFormView::AssertValid();
}

void CEjerforholdForm::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CEjerforholdForm message handlers

void CEjerforholdForm::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	IUnknown * pUnk = NULL;
	ULONG fetched = 0;

	try {
		m_pEnumEjerforhold.CreateInstance(__uuidof(EBASDLLLib::EnumEjerforhold));
		m_pEnumEjerforhold->Next(&pUnk, &fetched);
		while (NULL != pUnk) {
			EBASDLLLib::IEjerforholdPtr pEjerforhold = NULL;
			pUnk->QueryInterface(__uuidof(EBASDLLLib::IEjerforhold), (void**)&pEjerforhold);
			pUnk = NULL;

			m_ledere.AddString(pEjerforhold->Leder);
			pEjerforhold = NULL;

			m_pEnumEjerforhold->Next(&pUnk, &fetched);
		}

		pUnk = NULL;		

		m_pEnumPostNummer.CreateInstance(__uuidof(EBASDLLLib::EnumPostNummer));
		m_pEnumPostNummer->Next(&pUnk, &fetched);
		while (NULL != pUnk) {
			EBASDLLLib::IPostNummerPtr pPostNummer = NULL;
			pUnk->QueryInterface(__uuidof(EBASDLLLib::IPostNummer), (void**)&pPostNummer);
			pUnk = NULL;

			m_by.AddString(pPostNummer->By);
			pPostNummer = NULL;

			m_pEnumPostNummer->Next(&pUnk, &fetched);
		}

		pUnk = NULL;

		m_pEnumAdresseType.CreateInstance(__uuidof(EBASDLLLib::EnumAdresseType));
		m_pEnumAdresseType->Next(&pUnk, &fetched);
		while (NULL != pUnk) {
			EBASDLLLib::IAdresseTypePtr pAdresseType = NULL;
			pUnk->QueryInterface(__uuidof(EBASDLLLib::IAdresseType), (void**)&pAdresseType);			
			pUnk = NULL;

			BSTR temp; bstr_t holder;
			pAdresseType->get_AdresseType(&temp);
			holder = temp;
			
			m_adresseType.AddString(holder);
			pAdresseType = NULL;

			m_pEnumAdresseType->Next(&pUnk, &fetched);
		}

		// Mark selection
		int pos = m_adresseType.FindString(-1, "Institution");
		m_adresseType.SetCurSel(pos);
	} catch (...) {

	}

	UpdateData(FALSE);
}

void CEjerforholdForm::OnKillfocusEditFirmaPostnr() 
{
	UpdateData(TRUE);

	if ("" == m_postNr)
		return;

	try {
		bstr_t postal = bstr_t(m_postNr); 	
		postal = m_pEnumPostNummer->findBy(postal);

		int pos = m_by.FindString(-1, postal);
		m_by.SetCurSel(pos);
	} catch (...) {
		
	}

	UpdateData(FALSE);
}

void CEjerforholdForm::OnKillfocusComboFirmaBy() 
{
	CString str;

	UpdateData(TRUE);

	m_by.GetWindowText(str);
	int pos = m_by.FindString(-1, str);
	if (-1 == pos) 
		return;

	try {
		bstr_t postal = bstr_t(str); 
		postal = m_pEnumPostNummer->findPostNr(postal);

		m_postNr = CString((char*)postal);
	} catch (...) {
		
	}

	UpdateData(FALSE);
}

void CEjerforholdForm::OnDblclkListLedere() 
{
	UpdateData();
	CString txt; int sel = -1, pos = -1;

	try {		
		sel = m_ledere.GetCurSel();
		m_ledere.GetText(sel, txt);

		m_pEjerforhold = m_pEnumEjerforhold->findEjerforholdLeder(bstr_t(txt));
		if (NULL == m_pEjerforhold)
			return;

		m_guid		= (char*)m_pEjerforhold->GUID;
		m_leder		= (char*)m_pEjerforhold->Leder;

		m_pAdresse	= m_pEjerforhold->Adresse;
				
		m_firma		= (char*)m_pAdresse->Attention;
		m_adresse	= (char*)m_pAdresse->Gade;
		m_telefon	= (char*)m_pAdresse->Telefon;
		m_telefax	= (char*)m_pAdresse->Telefax;
		m_epost		= (char*)m_pAdresse->Email;

		pos = m_by.FindString(-1, (char*)m_pAdresse->By);
		m_by.SetCurSel(pos);

		m_postNr = (char*)m_pEnumPostNummer->findPostNr(m_pAdresse->By);				
	} catch (...) {

	}
	
	UpdateData(FALSE);
}

void CEjerforholdForm::OnRyd()
{
	UpdateData();

	try {
		m_pEjerforhold = NULL;
		m_pAdresse = NULL;

		m_guid		= "";
		m_leder		= "";			
		m_firma		= "";
		m_adresse	= "";
		m_telefon	= "";
		m_telefax	= "";
		m_epost		= "";
		m_postNr	= "";

		m_by.SetCurSel(-1);

	} catch (...) {

	}

	UpdateData(FALSE);
}

void CEjerforholdForm::OnGem()
{
	UpdateData();

	try {



	} catch (...) {

	}

	UpdateData(FALSE);
}

void CEjerforholdForm::OnSlet()
{
	UpdateData();

	try {
		if(NULL == m_pEjerforhold)
			return;

		m_pEjerforhold->Slet();
		m_pEjerforhold = NULL;
		m_pAdresse = NULL;

		m_guid		= "";
		m_leder		= "";			
		m_firma		= "";
		m_adresse	= "";
		m_telefon	= "";
		m_telefax	= "";
		m_epost		= "";
		m_postNr	= "";

		m_by.SetCurSel(-1);
	} catch (...) {

	}

	UpdateData(FALSE);
}